package club.beimeng.emailserver2.controller;


import club.beimeng.emailserver2.entity.EmailTemplate;
import club.beimeng.emailserver2.service.EmailTemplateService;
import club.beimeng.emailserver2.utils.R;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 小艾同学
 * @since 2021-09-29
 */
@RestController
@RequestMapping("/emailSever2/email-template")
public class EmailTemplateController { // 提供模板的增删改查

    @Autowired
    public EmailTemplateService emailTemplateService;

    @ApiOperation("添加模板html")
    @PostMapping("addTemplate")
    public R addTemplate(String template){

        Long templateCode = emailTemplateService.addTemplate(template);
        return R.ok().message("添加成功").data("templateCode",templateCode.toString());
    }

    @ApiOperation("删除模板")
    @DeleteMapping("deleteTemplate")
    public R deleteTemplate(String templateCode){

        boolean b = emailTemplateService.removeById(templateCode);
        return b?R.ok():R.error();
    }

    @ApiOperation("修改模板")
    @PutMapping("updateTemplate")
    public R updateTemplate(String templateCode,String template){

        EmailTemplate emailTemplate = new EmailTemplate();
        emailTemplate.setTid(Long.parseLong(templateCode));
        emailTemplate.setTemplate(template);
        boolean b = emailTemplateService.updateById(emailTemplate);
        return b?R.ok():R.error();
    }

    @ApiOperation("获取模板")
    @GetMapping("getTemplateById")
    public R getTemplateById(String templateCode){

        EmailTemplate template = emailTemplateService.getById(Long.parseLong(templateCode));
        if (template != null)
            return R.ok().data("template",template);
        else
            return R.error();
    }

    @ApiOperation("获取模板")
    @GetMapping("getTemplates")
    public R getTemplates(){

        List<EmailTemplate> list = emailTemplateService.list();
        if (list != null)
            return R.ok().data("templateList",list);
        else
            return R.error();
    }



}

